package ItemBased;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

public class GenerateCoOccurrences {

	public static class GenerateCoOccurrencesMap
    extends Mapper<LongWritable, LongPairArrayWritableComparable, LongPairWritableComparable, LongPairWritableComparable>{
	      
	    public void map(LongWritable key, LongPairArrayWritableComparable value,
	    		OutputCollector<LongPairWritableComparable, LongPairWritableComparable> output, Reporter reporter
	                    ) throws IOException, InterruptedException {
	    	
	    	LongPairWritableComparable[] array = value.getData();
	    	
	    	for(int i = 0; i < array.length; i++){
	    		for(int j = (i + 1); j < array.length; j++){
	    			output.collect(new LongPairWritableComparable(array[i].getFirst(), array[j].getFirst()), new LongPairWritableComparable(array[i].getSecond(), array[j].getSecond()));
	    		}
	    	}
	    	
	    }
	}
	
	
	public static class GenerateCoOccurrencesReduce
    extends Reducer<LongPairWritableComparable, LongPairWritableComparable, LongPairWritableComparable, LongWritable>{
	      
	    public void reduce(LongPairWritableComparable key, Iterator<LongPairWritableComparable> values,
	    		OutputCollector<LongPairWritableComparable, LongWritable> output, Reporter reporter
	                    ) throws IOException, InterruptedException {
	    	
	    	ArrayList<LongWritable> item1 = new ArrayList<LongWritable>();
	    	ArrayList<LongWritable> item2 = new ArrayList<LongWritable>();
	    	
	    	while(values.hasNext()){
	    		LongPairWritableComparable pair = values.next();
	    		item1.add(new LongWritable(pair.getFirst()));
	    		item2.add(new LongWritable(pair.getSecond()));
	    	}
	    	output.collect(key, new LongWritable(cosineSimilarity(item1, item2)));
	    }
	    
	    private long cosineSimilarity(ArrayList<LongWritable> one, ArrayList<LongWritable> two){
	    	return 0;
	    }
	}
	    
	    
	    
	    
	    
	    
	public static void main(String[] args) {
		

	}

}
